package
{
	import com.bit101.components.PushButton;
	import com.bit101.components.Window;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import com.bit101.components.InputText
	import flash.filesystem.File;
	import flash.events.NativeDragEvent;
	import flash.utils.ByteArray;
	import flash.desktop.NativeDragManager;
	
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.events.Event
	import flash.display.Bitmap;
	import flash.desktop.NativeDragManager;
	
	
	import flash.filesystem.FileStream;
	import flash.filesystem.FileMode;
	
	/**
	 * ...
	 * @author artem.kolesnikov
	 */
	public class Main extends Sprite
	{
		
		
		[Embed(source = "../assets/cpp-159.png")]
		private var imageSource:Class;
		
		private var cppImage:Bitmap;
		
		private var nameInputText:InputText;
		/*private var saveButton:PushButton;*/
		private	var window:Window;

		public function Main():void
		{
			window = new Window(this, 0, 0, "Drag here '.cpp' or '.h' files for duplicate")
			window.draggable = false;
			
			nameInputText = new InputText(window, 0, 10, "COPY_");
			nameInputText.scaleX = 2;
			nameInputText.scaleY = 2;
			
			/*saveButton = new PushButton(window, 0, 10, "save", onSaveClick );
			saveButton.width = 50;
			saveButton.height = nameInputText.height * nameInputText.scaleY * 0.5;
			saveButton.scaleX = 2;
			saveButton.scaleY = 2;*/
			
			cppImage = new imageSource();
			addChild(cppImage);
			
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDrop);
			this.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT, onDragExit);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onResize);
			this.addEventListener(Event.RESIZE, onResize);
		}
		
		private function onResize(event:Event):void {
			window.width = stage.stageWidth;
			window.height = stage.stageHeight;
			
			nameInputText.x = window.width / 2 - nameInputText.width;
			
			/*saveButton.x = nameInputText.x +nameInputText.width * nameInputText.scaleX + 10;*/
			
			cppImage.x = stage.stageWidth / 2 - cppImage.width / 2;
			cppImage.y = stage.stageHeight / 2 - cppImage.height / 2;
		}
		
		private function onDragIn(event:NativeDragEvent):void {
			NativeDragManager.acceptDragDrop(this);
		}
		
		private function onDrop(event:NativeDragEvent):void {
			doSome(event);
		}
		
		private function onDragExit(event:NativeDragEvent):void {
			trace("onDragExit");
		}
		
		private function doSome(event:NativeDragEvent):void {
			var dropFiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			for each (var file:File in dropFiles){
				switch (file.extension){
				 case "cpp" :
					trace("CPP");
					
				 break;
				 case "h" :
					trace("H");
					
				 break;
				 default:
					trace("OTHER");
					
				}
				trace(">>>", file.name, file.nativePath);
				duplicate(file);
			}
		}
		
		private function duplicate(file:File):void {
			trace("do duplicate");
			
			var fromName:String = file.name.substr(0, file.name.length - file.extension.length - 1);
			
			var toName:String = getNewName(fromName);
			nameInputText.text = toName;
		
			var fs:FileStream = new FileStream();
			fs.open(file, FileMode.READ);
			var data:String = fs.readUTFBytes(fs.bytesAvailable);	
			var newData:String = data.split(fromName).join(toName);
			fs.close();
			
			var file:File = File.desktopDirectory.resolvePath(toName + "." + file.extension);
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(newData);
			stream.close();
		}
		
		
		
		/*private function onSaveClick(event:Event):void {
			trace("save");
		}*/
		
		private function getNewName(name:String):String {
			var newName:String = name;
			if (name.substr(0, 2) == "BB" || name.substr(0, 2) == "BT" || name.substr(0, 2) == "BW") {
				newName = newName.substr(2, newName.length);
			}
			return "BW" + newName; // добавить потом выбор проекта
		}
	}
}